﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Utils.Common.CompressProj
{
   public class CommonFunctions
    {

        #region 单例模式

        private static CommonFunctions uniqueInstance;
        private static object _lock = new object();

        private CommonFunctions() { }
        public static CommonFunctions getInstance()
        {
            if (null == uniqueInstance)      //确认要实例化后再进行加锁，降低加锁的性能消耗。
            {
                lock (_lock)
                {
                    if (null == uniqueInstance)
                    {
                        uniqueInstance = new CommonFunctions();
                    }
                }
            }
            return uniqueInstance;
        }

        #endregion

        #region 进程

        /// <summary>
        /// 另起一进程执行命令
        /// </summary>
        /// <param name="exe">可执行程序(路径+名)</param>
        /// <param name="commandInfo">执行命令</param>
        /// <param name="workingDir">执行所在初始目录</param>
        /// <param name="processWindowStyle">进程窗口样式</param>
        /// <param name="isUseShellExe">Shell启动程序</param>
        public void ExecuteProcess(string exe, string commandInfo, string workingDir = "", ProcessWindowStyle processWindowStyle = ProcessWindowStyle.Hidden, bool isUseShellExe = false)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = exe;
            startInfo.Arguments = commandInfo;
            startInfo.WindowStyle = processWindowStyle;
            startInfo.UseShellExecute = isUseShellExe;
            if (!string.IsNullOrWhiteSpace(workingDir))
            {
                startInfo.WorkingDirectory = workingDir;
            }

            ExecuteProcess(startInfo);
        }

        /// <summary>
        /// 直接另启动一个进程
        /// </summary>
        /// <param name="startInfo">启动进程时使用的一组值</param>
        public void ExecuteProcess(ProcessStartInfo startInfo)
        {
            try
            {
                Process process = new Process();
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
                process.Close();
                process.Dispose();
            }
            catch (Exception ex)
            {
                throw new Exception("进程执行失败：\n\r" + startInfo.FileName + "\n\r" + ex.Message);
            }
        }

        /// <summary>
        /// 去掉文件夹或文件的只读属性
        /// </summary>
        /// <param name="objectPathName">文件夹或文件全路径</param>
        public void Attribute2Normal(string objectPathName)
        {
            if (true == Directory.Exists(objectPathName))
            {
                System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(objectPathName);
                directoryInfo.Attributes = FileAttributes.Normal;
            }
            else
            {
                File.SetAttributes(objectPathName, FileAttributes.Normal);
            }
        }

        #endregion 

    }
}
